package cn.com.ghy.android.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import cn.com.ghy.android.bean.LoginInfoBean;
import cn.com.ghy.android.bean.MatnrInfoBean;
import cn.com.ghy.android.bean.YGOrderBaseInfoBean;
import cn.com.ghy.android.bean.YGOrderDetailInfoBean;
import cn.com.ghy.android.bean.YGOrderInfoBean;
import cn.com.ghy.android.bean.YGQueryInfoBean;
import cn.com.ghy.android.util.CommonStatic;
import cn.com.ghy.android.util.CommonUtil;
import cn.com.ghy.android.util.XMLParseUtils;

/**
 * @软件著作权:金红叶纸业集团有限公司
 * @系统名称:SFA系统
 * @Description:营管订单服务类
 * @author wuz  @date 2012-01-12 10:19:58
 * @版本历史:v1.0
 * @Copyright:Copyright (c) 2011-2012 
 */

public class YgOrderOpeService {
	private String url = CommonStatic.URL_HEAD + "YgOrderOpe_Ar.asmx";

	private String methodName = "";
	private Map<String, String> params = null;

	private static final String DTINFO = "dtinfo";
	private static final String THETYPE = "thetype";
	private static final String ORDERSEARCH = "ordersearch";
	private static final String USERID = "userid ";
	private static final String PERNR = "pernr";
	private static final String ORDERID = "orderid";
	private static final String BISMT = "bismt";
	private static final String ORDERHEAD = "orderhead";
	private static final String ORDERDETAIL = "orderdetail";

	public List<YGQueryInfoBean> GetSlt(Map<String, String> info, String thetype) throws Exception {
		Map<String, String> dtinfoMap = new HashMap<String, String>();
		dtinfoMap.put("VKORG", "");
		dtinfoMap.put("KUNNR", "");
		dtinfoMap.put("KUNAG", "");
		dtinfoMap.put("VKGRP", "");
		dtinfoMap.put("VKBUR", "");
		dtinfoMap.put("VTWEG", "");
		dtinfoMap.put("SPART", "");
		dtinfoMap.put("AUART", "");
		dtinfoMap.put("STRASNO", "");
		dtinfoMap.put("WERKS", "");
		dtinfoMap.put("LGORT", "");
		dtinfoMap.put("BISMT", "");
		dtinfoMap.put("UNIT", "");
		dtinfoMap.put("VOLUM", "");
		dtinfoMap.put("MATNR", "");
		dtinfoMap.put("DNM", "");
		dtinfoMap.put("PERNR", "");
		dtinfoMap.put("SEARCH_VAL", "");
		dtinfoMap.put("SEARCH_TITLE", "");

		Iterator<String> it = info.keySet().iterator();
		while (it.hasNext()) {
			String key = it.next();
			dtinfoMap.put(key, info.get(key));
		}

		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		list.add(dtinfoMap);

		methodName = "GetSlt";

		String dtinfoXML = XMLParseUtils.createXML(list);
		dtinfoXML = CommonUtil.compressAndBase64ToString(dtinfoXML);

		params = new HashMap<String, String>();
		params.put(DTINFO, dtinfoXML);
		params.put(THETYPE, thetype);

		String result = HttpService.callWS(url, methodName, params);
		byte[] buffer = CommonUtil.base64AndDecompressToByteArray(result);

		List<YGQueryInfoBean> yGQueryInfoBeanList = XMLParseUtils.parseXML(buffer,
				YGQueryInfoBean.class);
		return yGQueryInfoBeanList;
	}

	public List<YGOrderInfoBean> GetOrderList(String orderid, String crbegindate, //
			String crenddate, LoginInfoBean loginInfo) throws Exception {
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		Map<String, String> ordersearch = new HashMap<String, String>();
		ordersearch.put("orderid", orderid == null ? "" : orderid);
		ordersearch.put("crbegindate", crbegindate);
		ordersearch.put("crenddate", crenddate);
		list.add(ordersearch);
		String ordersearchXML = XMLParseUtils.createXML(list);

		ordersearchXML = CommonUtil.compressAndBase64ToString(ordersearchXML);

		methodName = "GetOrderList";
		params = new HashMap<String, String>();
		params.put(ORDERSEARCH, ordersearchXML);
		params.put(USERID, loginInfo.getPernr());

		String result = HttpService.callWS(url, methodName, params);
		byte[] buffer = CommonUtil.base64AndDecompressToByteArray(result);

		List<YGOrderInfoBean> yGOrderInfoBeanList = XMLParseUtils.parseXML(buffer,
				YGOrderInfoBean.class);

		return yGOrderInfoBeanList;
	}

	public Map<String, Object> GetInitOrder(LoginInfoBean loginInfo) throws Exception {
		methodName = "GetInitOrder";
		params = new HashMap<String, String>();
		params.put(PERNR, loginInfo.getPernr());

		String result = HttpService.callWS(url, methodName, params);
		byte[] buffer = CommonUtil.base64AndDecompressToByteArray(result);

		List<YGOrderBaseInfoBean> yGOrderBaseInfoBeanList = XMLParseUtils.parseXML("dtmain", //
				buffer, YGOrderBaseInfoBean.class);
		List<YGOrderDetailInfoBean> yGOrderDetailInfoBeanList = XMLParseUtils.parseXML("dtdetail",//
				buffer, YGOrderDetailInfoBean.class);

		Map<String, Object> map = new HashMap<String, Object>();

		map.put("orderBase", yGOrderBaseInfoBeanList);
		map.put("orderDetail", yGOrderDetailInfoBeanList);

		return map;
	}

	public Map<String, Object> OrderShow(String orderid) throws Exception {
		methodName = "OrderShow";

		params = new HashMap<String, String>();
		params.put(ORDERID, orderid);

		String result = HttpService.callWS(url, methodName, params);
		byte[] buffer = CommonUtil.base64AndDecompressToByteArray(result);

		List<YGOrderBaseInfoBean> yGOrderBaseInfoBeanList = XMLParseUtils.parseXML("orderbase", //
				buffer, YGOrderBaseInfoBean.class);
		List<YGOrderDetailInfoBean> yGOrderDetailInfoBeanList = XMLParseUtils.parseXML( //
				"orderdetail", buffer, YGOrderDetailInfoBean.class);

		Map<String, Object> map = new HashMap<String, Object>();

		map.put("orderBase", yGOrderBaseInfoBeanList);
		map.put("orderDetail", yGOrderDetailInfoBeanList);

		return map;
	}

	public List<MatnrInfoBean> GetMatnrList(String bismt) throws Exception {
		methodName = "GetMatnrList";

		params = new HashMap<String, String>();
		params.put(BISMT, bismt);

		String result = HttpService.callWS(url, methodName, params);
		byte[] buffer = CommonUtil.base64AndDecompressToByteArray(result);

		List<MatnrInfoBean> matnrInfoBeanList = XMLParseUtils.parseXML(buffer, MatnrInfoBean.class);

		return matnrInfoBeanList;
	}

	public String OrderAdd(List<YGOrderBaseInfoBean> yGOrderBaseInfoBeanList,
			List<YGOrderDetailInfoBean> yGOrderDetailInfoBeanList) throws Exception {
		methodName = "OrderAdd";
		params = new HashMap<String, String>();

		String orderheadXML = XMLParseUtils.createXMLOfBean(yGOrderBaseInfoBeanList);
		orderheadXML = CommonUtil.compressAndBase64ToString(orderheadXML);

		String orderdetailXML = XMLParseUtils.createXMLOfBean(yGOrderDetailInfoBeanList);
		orderdetailXML = CommonUtil.compressAndBase64ToString(orderdetailXML);

		params.put(ORDERHEAD, orderheadXML);
		params.put(ORDERDETAIL, orderdetailXML);
		String result = HttpService.callWS(url, methodName, params);

		return result;
	}

	public String OrderEdit(List<YGOrderBaseInfoBean> yGOrderBaseInfoBeanList,
			List<YGOrderDetailInfoBean> yGOrderDetailInfoBeanList) throws Exception {
		methodName = "OrderEdit";
		params = new HashMap<String, String>();

		String orderheadXML = XMLParseUtils.createXMLOfBean(yGOrderBaseInfoBeanList);
		orderheadXML = CommonUtil.compressAndBase64ToString(orderheadXML);

		String orderdetailXML = XMLParseUtils.createXMLOfBean(yGOrderDetailInfoBeanList);
		orderdetailXML = CommonUtil.compressAndBase64ToString(orderdetailXML);

		params.put(ORDERHEAD, orderheadXML);
		params.put(ORDERDETAIL, orderdetailXML);
		String result = HttpService.callWS(url, methodName, params);

		return result;
	}
}
